/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.Feedback;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class FeedbackDAO {
  private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<Feedback> GetAll() {
        ArrayList<Feedback> listFeedBack = new ArrayList<Feedback>();
        Feedback feedback = null;
        try {

            strCommand = "select * from tbl_feedback";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
                feedback = new Feedback();

               feedback.setFeedbackId(rs.getInt("feedbackId"));
               feedback.setGuestName(rs.getString("guestName"));
               feedback.setGuestEmail(rs.getString("guestEmail"));
               feedback.setSubject(rs.getString("subject"));
               feedback.setContent(rs.getString("content"));
               feedback.setSentDate(rs.getString("sentDate"));
               feedback.setFeedbackStatus(rs.getInt("feedbackStatus"));

                listFeedBack.add(feedback);
            }
            return listFeedBack;
        } catch (SQLException ex) {
            Logger.getLogger(FeedbackDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listFeedBack;
    }


    public Feedback GetByID(int feedbackId) {

        Feedback feedback = new Feedback();
        try {

            strCommand = "select * from tbl_feedback WHERE feedbackId=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, feedbackId);
            rs = ps.executeQuery();
            while (rs.next()) {
               feedback.setFeedbackId(rs.getInt("feedbackId"));
               feedback.setGuestName(rs.getString("guestName"));
               feedback.setGuestEmail(rs.getString("guestEmail"));
               feedback.setSubject(rs.getString("subject"));
               feedback.setContent(rs.getString("content"));
               feedback.setSentDate(rs.getString("sentDate"));
               feedback.setFeedbackStatus(rs.getInt("feedbackStatus"));

            }
            return feedback;
        } catch (SQLException ex) {
            Logger.getLogger(FeedbackDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return feedback;
    }

    public ArrayList<Feedback> GetByStatus() {

        ArrayList<Feedback> listfeedback = new ArrayList<Feedback>();
        Feedback feedback = null;
        try {

            strCommand = "select * from tbl_feedback WHERE feedbackStatus= 1 ";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            rs = ps.executeQuery();
            while (rs.next()) {
               feedback = new Feedback();
               feedback.setFeedbackId(rs.getInt("feedbackId"));
               feedback.setGuestName(rs.getString("guestName"));
               feedback.setGuestEmail(rs.getString("guestEmail"));
               feedback.setSubject(rs.getString("subject"));
               feedback.setContent(rs.getString("content"));
               feedback.setSentDate(rs.getString("sentDate"));
               feedback.setFeedbackStatus(rs.getInt("feedbackStatus"));
               listfeedback.add(feedback);

            }
            return listfeedback;
        } catch (SQLException ex) {
            Logger.getLogger(PostTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listfeedback;
    }

    public int GetNotRead() {

        int i = 0;
        try {

            strCommand = "SELECT count(*) AS NOREAD FROM tbl_feedback WhERE feedbackStatus = 1";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            rs = ps.executeQuery();
            while(rs.next()){
               i = rs.getInt("NOREAD");
            }

            return i;
        } catch (SQLException ex) {
            Logger.getLogger(FeedbackDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return i;
    }

    public boolean changeStatus(int feedbackId) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_feedback SET feedbackStatus = 0 WHERE feedbackId = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, feedbackId);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

  
    public boolean deleteFeedback(int feedbackId)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_feedback WHERE feedbackId = ? AND feedbackStatus = 0 ";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, feedbackId);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
